package com.tao.controller.webpageController;

import com.tao.result.Result;
import com.tao.service.ReportService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

@CrossOrigin(origins = "http://localhost:7000/")
@RestController
@Slf4j
@RequestMapping("/webPage/report")
public class ReportController {
    @Autowired
    ReportService reportService;

    @GetMapping("/turnoverStatistic")
    public Result turnoverStatistic(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate startDate,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate endDate) {
        return Result.success(reportService.turnoverStatistic(startDate, endDate));
    }

    @GetMapping("/userStatistic")
    public Result userStatistic(@DateTimeFormat(pattern = "yyyy-MM-dd")
                                LocalDate startDate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd")
                                LocalDate endDate) {
        return Result.success(reportService.userStatistic(startDate, endDate));
    }

    @GetMapping("/orderStatistic")
    public Result orderStatistic(@DateTimeFormat(pattern = "yyyy-MM-dd")
                                     LocalDate startDate,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd")
                                     LocalDate endDate){
        return Result.success(reportService.orderStatistic(startDate, endDate));
    }

    @GetMapping("/top10")
    public Result productStatistic(@DateTimeFormat(pattern = "yyyy-MM-dd")
                                       LocalDate startDate,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd")
                                       LocalDate endDate){
        return Result.success(reportService.productStatistic(startDate, endDate));
    }

    @GetMapping("/export")
    public Result export(HttpServletResponse response) throws IOException {
        reportService.exportBusinessDate(response);
        return Result.success();
    }
}
